package com.snake.window.snake;

import com.snake.window.MainView;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Random;

/**
 * 2 * @Author: Conph
 * 3 * @Date: 2019/8/6 17:16
 * 4
 */
public class Food {
    private JButton foodBlock;
    private int foodSildeLength;
    private List<JButton> wallList;
    private List<JButton> snakeList;
    private int gameAreaWidth;
    private int gameAreaHeight;
    private int wallX;
    private int wallY;
    private int energy;
    private Color foodColor;
    private MainView mainView;
    private JPanel gameArea;
    private int foodX;
    private int foodY;
    private Random r1 = new Random();

    public Food(Snake curSnake){
        foodBlock = new JButton();
        foodBlock.setEnabled(false);
        mainView = curSnake.getMainView();
        foodBlock.setSize(mainView.getBlockWith(), mainView.getBlockHeight());
        gameArea = mainView.getGameArea();
        foodSildeLength = mainView.getBlockWith();
        wallList = mainView.getWallList();
        snakeList = curSnake.getSnakeBlockList();
        gameAreaWidth = (mainView.getWallWidthBlockCount() - 2) * mainView.getBlockWith();
        gameAreaHeight = (mainView.getWallHeightBlockCount() - 2) * mainView.getBlockHeight();
        wallX = mainView.getWallX() + mainView.getBlockWith();
        wallY = mainView.getWallY() + mainView.getBlockHeight();

        energy = 1;
        foodColor = new Color(0, 255, 255);

        gameArea.add(foodBlock);
        foodRefresh();
    }

    public Point getFoodPoint() {
        return foodBlock.getLocation();
    }

    public void foodRefresh() {
        Point point = makeFoodPoint();
        foodBlock.setLocation(point);
        System.out.println("food Point:" + point);
        foodBlock.setVisible(true);
    }

    public void disappearFood() {
        foodBlock.setVisible(false);
    }

    private Point makeFoodPoint() {
        int widthRandom = mainView.getWallWidthBlockCount() - 2;
        int heightRandom = mainView.getWallHeightBlockCount() - 2;

        Point foodPoint = new Point(r1.nextInt(widthRandom) * mainView.getBlockWith() + wallX
                ,r1.nextInt(heightRandom) * mainView.getBlockHeight() + wallY);

        for(JButton jbtnBody : snakeList) {
            Point curBodyPoint = jbtnBody.getLocation();
            if(foodPoint.equals(curBodyPoint)) {
                return makeFoodPoint();
            }
        }
        return foodPoint;
    }

    public int getEnergy() {
        return energy;
    }
}
